package com.hl.mapper;

import com.hl.pojo.Question;
import com.hl.pojo.Reply;
import com.hl.pojo.SystemUser;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface ReplyMapper {
    @Select("select r.*, r.system_user_id id1, r.question_id id2,r.reply_user_id id3 from reply r where del = 0")
    @Results({
            @Result(property = "systemUser" ,column = "id1",
                one = @One(select = "b1")),
            @Result(property = "question" ,column = "id2",
                one = @One(select = "b2")),
            @Result(property = "systemUser" ,column = "id3",
                one = @One(select = "b3"))
    })
    public List<Reply> findAll();

    @Select("select * from system_user where id = #{id}")
    public SystemUser b1(Integer id);
    @Select("select * from question where id = #{id}")
    public Question b2(Integer id);
    @Select("select * from system_user where id = #{id}")
    public SystemUser b3(Integer id);


    @Insert("insert into reply (reply_content, system_user_id, question_id,reply_user_id,time) values (#{replyContent}, #{systemUserId}, #{questionId},#{replyUserId},#{time})")
    public Integer add(Reply r);

    @Update("update reply set reply_content=#{replyContent}, system_user_id=#{systemUserId}, question_id=#{questionId}, del=#{del} where id=#{id}")
    public Integer update(Reply r);

    @Update("update  reply  set del = 1 where id = #{id}")
    public Integer delete(Integer id);

    @Select("select * from reply where id = #{id} and del != 1")
    public Reply findById(Integer id);

    @Select("select r.*, r.system_user_id userid, r.reply_user_id userid1 from reply r where del != 1 and question_id = #{questionId} order by time desc")
    @Results({
            @Result(property = "username",column = "userid" ,
                    one = @One(select = "a1")),
            @Result(property = "replyUserName",column = "userid1" ,
                    one = @One(select = "a1")),

    })
    public List<Reply> findByQuestionId(Integer questionId);

    @Select("select r.*, r.system_user_id userid from reply r where del != 1 and system_user_id = #{userId} order by time desc limit 10")
    @Results({
            @Result(property = "username",column = "userid" ,
                    one = @One(select = "a1"))
    })
    public List<Reply> findByUserId(Integer userId);
    @Select("select username from system_user where id = #{id}")
    public String a1(Integer id);

}
